package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.common.response.JsonResult;
import cn.tedu._05weibo.common.response.StatusCode;
import cn.tedu._05weibo.mapper.CommentMapper;
import cn.tedu._05weibo.pojo.dto.CommentDto;
import cn.tedu._05weibo.pojo.entity.Comment;
import cn.tedu._05weibo.pojo.vo.CommentVo;
import cn.tedu._05weibo.pojo.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
@Api(tags ="03.评论模块")
@Slf4j
@RestController
@RequestMapping(value = "/v1/comment/")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    /**
     * 1.发布评论
     * @param commentDto
     * @param session
     * @return
     */
    @PostMapping(value = "insert")
    @ApiOperation(value = "发布评论功能")
    public JsonResult insert(@RequestBody CommentDto commentDto, @ApiIgnore HttpSession session){
        log.debug("commentDto = " + commentDto);
        UserVo userVO = (UserVo) session.getAttribute("user");
        if (userVO == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDto,comment);
        comment.setUserId(userVO.getId());
        comment.setCreated(new Date());
        commentMapper.insert(comment);
        return JsonResult.ok();
    }
    /**
     * 2.获取评论
     */
    @ApiOperation(value = "获取评论功能")
    @GetMapping("selectByWeiboId")
    @ApiImplicitParam(name = "id",value = "微博ID",required = true,dataType = "Long")
    public JsonResult selectByWeiboId(Long id){
        List<CommentVo> commentVos = commentMapper.selectByWeiboId(id);
        return JsonResult.ok(commentVos);
    }

}
